package com.n5x.controller;

import com.github.pagehelper.PageHelper;
import com.n5x.common.base.JsonModel;
import com.n5x.model.discount.DiscountCash;
import com.n5x.model.discount.DiscountCashExample;
import com.n5x.service.IDiscountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 优惠信息接口Controller
 * 不需要用户token
 */
@Controller
@RequestMapping("/discount")
public class DiscountController extends BaseController {

    @Autowired
    IDiscountService iDiscountService;

    @RequestMapping(value = "/list")
    @ResponseBody
    public JsonModel discountCashList(@RequestParam(value = "pageNum", required = false) Integer pageNum, @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        logger.info("[查询优惠信息列表]");
        DiscountCashExample.Criteria criteria2 = new DiscountCashExample().createCriteria();
        criteria2.andVaildTimeIsNull().andDisableEqualTo(Boolean.FALSE).andTypeEqualTo(DiscountCash.Type.充值优惠.name()).andIsShowEqualTo(Boolean.TRUE);
        List<DiscountCash> list = iDiscountService.findDiscountCashList(criteria2);
        DiscountCashExample.Criteria criteria1 = new DiscountCashExample().createCriteria();
        criteria1.andVaildTimeGreaterThanOrEqualTo(new Date()).andDisableEqualTo(Boolean.FALSE).andTypeEqualTo(DiscountCash.Type.充值优惠.name()).andIsShowEqualTo(Boolean.TRUE);
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<DiscountCash> vaildList = iDiscountService.findDiscountCashList(criteria1);
        list.addAll(vaildList);
        return getResult(list);
    }
}